//
//  CustomView.swift
//  MyCloset
//
//  Created by 牟好好 on 2024/11/12.
//

import SwiftUI

struct CustomView: View {
    @Environment(\.dismiss) var dismiss
    @EnvironmentObject var vm: ClothStylesViewModel
    @State private var selectedTab = "上装"
    @State private var isScroll = false
    @State private var text: String = ""
    @ObservedObject var coreVm = CoreDataViewModel.shared
    @StateObject var fileVm = FileManagerViewModel()
    @State private var isShowError = false
    @FocusState private var isFocused: Bool
    
    var body: some View {
        VStack(spacing: 20) {
            Group {
                TopNavigationBar(title: "修改样式") {
                    dismiss()
                }
                
                TopTabBarView(selectedTab: $selectedTab, tabBarColor: .systemBlue)
                
                TextField("新增\(selectedTab)样式(不超过20字)", text: $text)
                    .focused($isFocused)
                    .padding()
                    .font(.system(size: 18, weight: .bold))
                    .frame(height: 55)
                    .background(Color("cardColor"))
                    .cornerRadius(10)
                    .padding(.horizontal)
                    .autocorrectionDisabled()
                    .autocapitalization(.none)
                // 更改右下角的按钮显示文字
                    .submitLabel(.done)
//                // 覆盖掉点击时隐藏键盘
//                    .gesture(
//                        TapGesture().onEnded { _ in }
//                    )
                
                Group {
                    Button {
                        // 不为空且不超过20字且不重复视为成功
                        if text.count < 21 && !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
                            let success = vm.addStyle(to: selectedTab, newStyle: text)
                            isShowError = !success
                            if success {
                                isScroll.toggle()
                            }
                        } else {
                            isShowError = true
                        }
                        text = ""
                    } label: {
                        VStack {
                            Text("保存")
                                .foregroundStyle(.white)
                                .font(.system(size: 20, weight: .bold))
                                .frame(width: boundsWidth * 0.8, height: boundsHeight * 0.06)
                                .background(.blue)
                                .cornerRadius(10)
                            
                            if isShowError {
                                Text("输入为空/样式已经存在/字数超限")
                                    .foregroundStyle(.red)
                                    .font(.footnote)
                            }
                        }
                    }
                }
                .frame(height: boundsHeight * 0.07)
            }
            
            VStack {
                StyleListView(selectedTab: $selectedTab, isScroll: $isScroll)
                Spacer()
            }
            .frame(maxWidth: .infinity)
            // 添加背景确保手势被触发
            .background(.subAccent)
            .onTapGesture {
                isFocused = false
            }
        }
    }
}

#Preview {
    CustomView()
        .environmentObject(ClothStylesViewModel())
}

#Preview {
    CustomView()
        .preferredColorScheme(.dark)
        .environmentObject(ClothStylesViewModel())
}
